import random #Allows the random module to be used


moves=["Rock","Paper","Scissors"]   #Defines moves

playerscore=0 #Initialises the score variables
cpuscore=0
timesplayed=0

def runCpuMove():
    return random.choice(moves) #Picks a random move for the cpu

def play():
    global cpuscore     #Allows cpu score to be used in the function
    global playerscore  #Allows player score to be used in the function
    global timesplayed  #Allows player score to be used in the function
    
    playerInput=input("What would you like to do? [rock/paper/scissors]\n") #Gets the players move
    
    if(playerInput == "rock" or playerInput == "Rock" or playerInput == "r"): #Logic for rock
        cpuMove = runCpuMove() #Get random cpu move
        if (cpuMove == "Rock"):
            print("Computer chose rock, it's a draw")

        if (cpuMove == "Paper"):
            print("Computer chose paper, you lose")
            cpuscore=cpuscore+1
            
        if (cpuMove == "Scissors"):
            print("Computer chose scissors, you win")
            playerscore=playerscore+1

        timesplayed=timesplayed+1 #Counts times played

    elif(playerInput == "paper" or playerInput == "Paper" or playerInput == "p"): #Logic for paper
        cpuMove = runCpuMove() #Get random cpu move
        if (cpuMove == "Rock"):
            print("Computer chose rock, you win")
            playerscore=playerscore+1
            
        if (cpuMove == "Paper"):
            print("Computer chose paper, it's a draw")
            
        if (cpuMove == "Scissors"):
            print("Computer chose scissors, you lose")
            cpuscore=cpuscore+1

        timesplayed=timesplayed+1 #Counts times played
        
    elif(playerInput == "scissors" or playerInput == "Scissors" or playerInput == "s"): #Logic for scissors
        cpuMove = runCpuMove() #Get random cpu move
        if (cpuMove == "Rock"):
            print("Computer chose rock, you lose")
            cpuscore=cpuscore+1
    
        if (cpuMove == "Paper"):
            print("Computer chose paper, you win")
            playerscore=playerscore+1
            
        if (cpuMove == "Scissors"):
            print("Computer chose scissors, it's a draw")

        timesplayed=timesplayed+1 #Counts times played
        
    else: #If the player didn't write rock, paper or scissors
        print("Input was invalid")

    print("Player's Points: " +str(playerscore) +"   Computer's Points: " +str(cpuscore)) #Displays live score

times=input("How many games is it out of: [As a number 1/2/3...]\n")

try:                #Complicated exception handling bit, that makes sure the user's input can be converted to an int
    times=int(times)#Attempts to convert
except ValueError:  #If it can't:
        print("Invalid Input, please re-enter an integer.") #Says is incorrect

        while(type(times) != int): #While it is incorrect
            times=input("How many games is it out of: [As a number 1/2/3...]\n") #Asks to be re-entered
            try: #Tries to convert
                times=int(times)  
            except ValueError:  #If it can't
                print("Invalid Input, please re-enter an integer.") #Says is invalid

while(playerscore <= float(times/2)) and cpuscore <= float(times/2) and timesplayed < int(times):
    #Loops as long as we still have games left and both can still win

    print("\n\nRound " +str(int(timesplayed+1)) +")")   #Displays the round
    play()   #Run the game

print("\n\nThe Game is over with the scores at:")
print("Player's Points: " +str(playerscore) +"   Computer's Points: " +str(cpuscore)) #Show scores

f = open('RPSOutput.txt', 'w')
f.write("Last game of rock paper scissors scores: \n Player's Points: " +str(playerscore) +"   Computer's Points: " +str(cpuscore))
f.close()
